package net.zjitc.dao.Impl;

import net.zjitc.dao.UserDao;
import net.zjitc.domain.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import utils.dbutils.C3P0utils;

import java.sql.SQLException;

/**
 * Created by guo on 2017/12/3.
 */
public class UserDaoImpl implements UserDao {
    public User findAlluser(String username, String password) {
        QueryRunner queryRunner=new QueryRunner(C3P0utils.getDs());
        String sql="select * from user";
        User user=null;
        try {
            user= (User) queryRunner.query(sql,new BeanHandler(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    public User findByCode(String code) {
        QueryRunner runner = new QueryRunner(C3P0utils.getDs());
        String sql = "select * from user where code=?";
        try {
            User user = (User) runner.query(sql,new BeanHandler(User.class),code);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int update(User user) {
        QueryRunner runner = new QueryRunner(C3P0utils.getDs());
        String sql = "update user set state=?,code=? where uid=?";
        try {
            int count = runner.update(sql,user.getState(),user.getCode(),user.getUid());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}

